package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.web.domain.bo.SysJobBo;
import org.dromara.web.domain.vo.SysJobVo;
import org.dromara.web.service.JobService;
import org.dromara.web.service.impl.JobServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务Controller
 *
 * @author Zou
 */
@Validated
@RestController
@SaIgnore
@RequiredArgsConstructor
@RequestMapping("/job")
@Slf4j
public class JobController extends BaseController {
    private final JobService jobService;
    /**
     * 指定methodName特定的cron表达式
     *
     * @param methodName methodName
     * @return cron表达式
     */
    @SaCheckPermission("job:job:methodName")
    @GetMapping("/getCron/{methodName}/{serviceName}/{serviceMethod}")
    public String getCron(@PathVariable String methodName, @PathVariable String serviceName, @PathVariable String serviceMethod) {
        // 获取 cron 表达式
        return jobService.getJobCron(methodName, serviceName, serviceMethod);
    }


    /**
     * 查询job信息列表
     */
    @SaCheckPermission("job:job:list")
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<SysJobVo> list(SysJobBo bo, PageQuery pageQuery) {
        return jobService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取job信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("job:job:query")
    @SaIgnore
    @GetMapping("/{id}")
    public R<SysJobVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(jobService.queryById(id));
    }

    /**
     * 新增job信息
     */
    @SaCheckPermission("job:job:add")
    @SaIgnore
    @Log(title = "job信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysJobBo bo) {
        return toAjax(jobService.insertByBo(bo));
    }

    /**
     * 修改job信息
     */
    @SaCheckPermission("job:job:edit")
    @SaIgnore
    @Log(title = "job信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysJobBo bo) {
        return toAjax(jobService.updateByBo(bo));
    }

    /**
     * 删除job信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("job:job:remove")
    @SaIgnore
    @Log(title = "Job信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(jobService.deleteWithValidByIds(List.of(ids), true));
    }
}
